package prefix.platform.email;

import java.util.HashMap;
import java.util.Map;

import play.Logger;
import prefix.platform.Constants;
import prefix.platform.email.AbstractEmailEngine.SEVERITY;
import prefix.util.OverallUtil;

public class EmailEngine extends AbstractEmailEngine {

    public EmailEngine() {

    }

    /**
     * Send admin alert with the specified severity
     */
    public static void sendAdminAlert(SEVERITY severity, String caller, String subject, String msg) {

        try { // Never fail because of email

            Map<String, String> vars = new HashMap<String, String>();
            vars.put("$CALLER$", caller);
            vars.put("$PROC_SRC$", INSTANCE);
            vars.put("$MESSAGE$", msg);
            vars.put("$CALL_STACK$", "");
            if (severity == SEVERITY.ERROR && msg.indexOf(".java:") <= 0) {
                vars.put("$CALL_STACK$", OverallUtil.getCallStackAsString("\n...", 4));
            }

            vars.put("$FROM_ADDRESS$", FROM_ADDRESS);
            vars.put("$FROM_NAME$", FROM_NAME);
            vars.put("$SUBJECT$", severity.toString() + ":" + INSTANCE + ":" + subject);

            sendTemplatedMessage(EMAIL_LIST_NOTIFY_ADMIN, null, vars, EmailTemplate.AdminAlert,
                    QueuedEmailEngine.ProcFlag.IMMEDIATE, null);

        } catch (Throwable t) {
            Logger.of(Constants.APPLICATION).error(" ", t);
            t.printStackTrace();
        }
    }

}
